import cy_request
import cy_config
import cy_operate
import time
import cy_log

# 平多持仓
def sellMoreRisk():

    # 平币种的仓 这里循环平仓
    is_null = False

    cy_log.printAndInLog_more(" 开始平多仓 ")

    local_symbol = cy_config.symbol

    if '-' in local_symbol:
        pass
    else:
        local_symbol = local_symbol.replace('USDT', '-USDT')

    while not is_null:

        # 加载持仓
        cy_request.initMyRisk()

        more_risks = []

        for risk in cy_request.my_postion_risks:
            
            positionSide = risk['posSide']
            symbol = risk['instId']

            if positionSide == 'long' and (local_symbol in symbol):

                more_risks.append(risk)

        if len(more_risks):

            # 市价全平
            cy_log.printAndInLog_more(" 存在持仓，开始平 ")

            cy_operate.sellOrder_more_risk(cy_config.symbol)

        else:
            is_null = True
            cy_log.printAndInLog_more(cy_config.symbol + ' 无持仓')

        time.sleep(1)

# 平空持仓
def sellLessRisk():

    # 平币种的仓 这里循环平仓
    is_null = False

    cy_log.printAndInLog_less(" 开始平空仓 ")

    local_symbol = cy_config.symbol

    if '-' in local_symbol:
        pass
    else:
        local_symbol = local_symbol.replace('USDT', '-USDT')

    while not is_null:

        # 加载持仓
        cy_request.initMyRisk()

        less_risks = []

        for risk in cy_request.my_postion_risks:
            
            positionSide = risk['posSide']
            symbol = risk['instId']

            if positionSide == 'short' and (local_symbol in symbol):

                less_risks.append(risk)

        if len(less_risks):

            cy_log.printAndInLog_less(" 存在，开始平空仓 ")
            cy_operate.sellOrder_less_risk(cy_config.symbol)

        else:
            is_null = True
            cy_log.printAndInLog_less(" 无持仓 ")

        time.sleep(1)

# 撤销币种所有方向为long的单
def cencelSymbolLongOrder():
    
    try:
        # 这里循环检测一下是否已经把这个币种的单给撤销了
        isHaskOrder = True

        local_symbol = cy_config.symbol

        if '-' in local_symbol:
            pass
        else:
            local_symbol = local_symbol.replace('USDT', '-USDT')

        while isHaskOrder:
            
            need_celcel_orders = []

            cy_request.initMyOrders()
            
            # 查看是否还存在这个币种的挂单
            for order in cy_request.my_postion_orders:

                time.sleep(cy_config.min_sleep_amount)

                order_symbol = order['instId']
                order_positionSide = order['posSide']
                order_side = order['side']

                if (local_symbol in order_symbol) and order_positionSide == 'long':

                    need_celcel_orders.append(order)

            if len(need_celcel_orders):

                # 撤销币种的挂单
                cencel_items = []

                for cencal_order in need_celcel_orders:

                    time.sleep(cy_config.min_sleep_amount)

                    ordId = cencal_order['ordId'] # 委托单id
                    instId = cencal_order['instId'] # 产品
             
                    postion = {
                        'ordId' : ordId,
                        'instId' : instId
                    }

                    cencel_items.append(postion)

                cy_log.printAndInLog_more('当前需要平的单：' + str(cencel_items))

                cy_request.cencel_orderwithId(cencel_items, True)
            else:
                # '已经不存在符合条件的buy单，结束'
                isHaskOrder = False

            time.sleep(1)

    except Exception as e:
        pass
    else:
        pass

# 撤销币种所有方向为short的单
def cencelSymbolShortOrder():

    # 这里循环检测一下是否已经把这个币种的单给撤销了
    isHaskOrder = True

    local_symbol = cy_config.symbol

    if '-' in local_symbol:
        pass
    else:
        local_symbol = local_symbol.replace('USDT', '-USDT')

    while isHaskOrder:
        
        need_celcel_orders = []

        cy_request.initMyOrders()

        # 查看是否还存在这个币种的挂单
        for order in cy_request.my_postion_orders:

            time.sleep(cy_config.min_sleep_amount)

            order_symbol = order['instId']
            order_positionSide = order['posSide']
            order_side = order['side']

            if (local_symbol in order_symbol) and order_positionSide == 'short':

                need_celcel_orders.append(order)

        if len(need_celcel_orders):
            
            # 撤销币种的挂单
            cencel_items = []

            for cencal_order in need_celcel_orders:
                
                time.sleep(cy_config.min_sleep_amount)

                ordId = cencal_order['ordId'] # 委托单id
                instId = cencal_order['instId'] # 产品
            
                postion = {
                    'ordId' : ordId,
                    'instId' : instId
                }

                cencel_items.append(postion)
            
            cy_log.printAndInLog_less('当前需要撤销的单：' + str(cencel_items))

            cy_request.cencel_orderwithId(cencel_items, True)

        else:
            # '已经不存在符合条件的buy单，结束'
            isHaskOrder = False

        time.sleep(1)